/*
 * Created on 23.02.2004
 * Local Projects Database
 * Mihai Postelnicu (mihai@ro-gateway.org)
 * (c) 2003 by eRomania Gateway
 */
package ro.gateway.aida.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.obj.ActivityRelation;
import ro.gateway.aida.obj.db.ActivityDB;
import ro.gateway.aida.obj.db.GenericActivityPropertyDB;
import ro.gateway.aida.obj.searchrows.ActivitySelectorRow;
import ro.gateway.aida.struts.MultiTilesAction;
import ro.gateway.aida.struts.form.ActivitySelectorForm;
import ro.gateway.aida.usr.User;
import ro.gateway.aida.usr.UserUtils;
import ro.gateway.aida.utils.PageToken;
import ro.xblue.translator.TranslatorUtils;


/**
 * @author Mihai Postelnicu<p>
 *  Searches & selects project objects
 *  e-mail (mihai@ro-gateway.org)<br>
 * (c) 2003 by eRomania Gateway<p>
 * @version 2.0
 */
public class ActivitySelectorAction extends MultiTilesAction {
    private  ActivityDB adb;
    private  GenericActivityPropertyDB gdb;

    public ActionForward modePrepare(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        User user = UserUtils.getLogged(request, response);

        if (user == null) {
            return mapping.findForward("plslogin");
        }

        if (adb == null || gdb==null) {
            adb = ActivityDB.getManager(token);
			gdb = GenericActivityPropertyDB.getManager(token);
        }

        //reset last search:
        
        return modeSelect(mapping, form, request, response);
    }

    public ActionForward modeSave(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();

        ActivitySelectorForm f = (ActivitySelectorForm) form;
        if(f.getSelectedActivityId()==-1) return modeView(mapping,form,request,response);
        ActivityRelation ar = new ActivityRelation();
        ar.setRelation(f.getRelationType());
        ar.setRelto_activity_id(f.getSelectedActivityId());
        ar.setRelToTitle(gdb.getFirstForActivity("gen_titles",ar.getRelto_activity_id()));

        if (ar == null)
            session.removeAttribute(ActivityRelation.SELECTED);
        else
            session.setAttribute(ActivityRelation.SELECTED, ar);

        request.setAttribute("save", "save");
		session.removeAttribute(ActivitySelectorRow.LIST);
        return mapping.getInputForward();
    }

    public ActionForward modeSearch(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        ActivitySelectorForm eaf = (ActivitySelectorForm) form;
        HttpSession session = request.getSession();
        String lang = TranslatorUtils.getCurrentLanguage(request,
                getServlet().getServletContext()).getLanguage();
        List l = adb.selectorSearch(eaf.getSearchText(),lang);
        session.setAttribute(ActivitySelectorRow.LIST, l);

        return modeView(mapping, form, request, response);
    }

    public ActionForward modeView(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        HttpSession session = request.getSession();
        List l = (List) session.getAttribute(ActivitySelectorRow.LIST);
        PageToken.tokenizeList( request,  sc, l, "ActivitySearchResults");
        
        return mapping.getInputForward();
    }

    public ActionForward modeSelect(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        if (request.getParameter("save") != null) {
            return modeSave(mapping, form, request, response);
        }

        if (request.getParameter("search") != null) {
            return modeSearch(mapping, form, request, response);
        }

        if (request.getParameter("cancel") != null) {
            HttpSession session = request.getSession();
            request.setAttribute("cancel", "cancel");
            session.removeAttribute(ActivitySelectorRow.LIST);
            return mapping.getInputForward();
        }

        return modeView(mapping, form, request, response);
    }
}
